/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 15:57:55
 */
import { getQueryParam, http, QueryParamKeys } from '@amcax/base';
import axios from 'axios';
import { httpV3 } from './httpV3';

export const httpApi = {
  // ====== AI推理 ======
  aiCompletions: async (data: any): Promise<any> =>
    http.post(`/api/next/v1/completions`, data),
  aiClearMemory: async (): Promise<any> =>
    http.post(`/api/next/v1/clear_memory`),
  aiChat: async (data: any): Promise<any> => http.post(`/aichat`, data),

  aiCompletionsV2: async (data: any): Promise<any> =>
    axios.post('/aichat', data),

  // ====== AI推理 ======

  // ======== 构件库 ========
  /**
   * @deprecated v3版本已弃用
   */
  getComponentCategories: async () =>
    http.get(`/api/v1/cad/components/categories`),
  /**
   * @deprecated v3版本已弃用
   */
  getComponents: (data: any) =>
    http.get(`/api/v1/cad/components`, { params: data }),
  /**
   * @deprecated v3版本已弃用
   */
  createComponent: (data: any) => http.post(`/api/v1/cad/components`, data),
  /**
   * @deprecated v3版本已弃用
   */
  updateComponent: (data: any) =>
    http.put(`/api/v1/cad/component/${data.uuid}`, data),
  /**
   * @deprecated v3版本已弃用
   */
  deleteComponent: (uuid: string) =>
    http.delete(`/api/v1/cad/component/${uuid}`),
  /**
   * @deprecated v3版本已弃用
   */
  checkComponentName: (data: any) =>
    http.post(`/api/v1/cad/components/checkName`, data),
  /**
   * @deprecated v3版本已弃用
   */
  genComponentName: (uuid: string) =>
    http.post(`/api/v1/cad/components/genName`, { projectUUID: uuid }),

  // ========end 构件库 ========

  // ======= 文档相关 ========
  // 根据文档uuid查询详情
  getDocumentInfoByUuid: (
    collectionUuid = getQueryParam<string>(QueryParamKeys.COLLECTION_ID),
    documentUuid = getQueryParam<string>(QueryParamKeys.DOCUMENT_ID),
    version = getQueryParam<number>(QueryParamKeys.VERSION),
  ) =>
    httpV3.post<CadDocument.DocumentInfo>(
      `/api/v3/documents/detailInfoByUuid`,
      {
        collectionUuid,
        documentUuid,
        version,
      },
    ),
  createDocument: (params: CadDocument.NewDocumentParams) => {
    return httpV3.post<CadDocument.NewDocumentResponse>(
      `/api/v3/documents/create`,
      params,
    );
  },
  // collections and documents
  getDocuments: (
    colIntId: string,
    catchElectronModal: boolean = false,
    params?: {
      ref: boolean;
      pid: string;
      pv: string;
    },
  ) => {
    let url = `/api/v3/documents/list?collectionId=${colIntId}`;
    if (params) {
      url = `${url}&ref=${params?.ref}&pid=${params?.pid}&pv=${params?.pv}`;
    }
    return httpV3.get<CadDocument.DocumentList>(url, {
      headers: {
        'catch-electron-modal': catchElectronModal,
      },
    });
  },

  exportAmxFile: (data: {
    collectionId: string;
    documentId: string;
    version: string;
  }) =>
    http.post(`/api/v3/documents/syncExport`, data, {
      responseType: 'arraybuffer',
    }), //这个不可用封装的v3， 因为返回数据不是标准的返回体 是文件流
  updateThumbnail: async (
    collectionId: string,
    documentId: string,
    base64: string,
  ) => {
    if (collectionId && documentId && base64) {
      const img = base64.split('base64,')[1];
      httpV3.post(`/api/v3/cad/doc/thumbnail`, {
        img,
        documentId,
        collectionId,
      });
    }
  },
  // 用户信息
  listUsers: (userIds: string[]) =>
    httpV3.post(`/api/v3/cad/listUsers`, { userUuids: userIds }),
  // 获取cam文档三角化数据和刀轨数据
  getCamData: (params: {
    documentUuid: string;
    version: string;
    params: string;
  }) => httpV3.post(`/api/v3/cad/cam/property`, { ...params }),

  // 获取单位配置
  getUnit: () => httpV3.post(`/api/v3/cad/user/units`),
  // 获取导出文件路径
  getExportPath: async (documentUuid: string, format: string) => {
    return httpV3.post<HttpApiSpace.AllocatePath>(`/api/v3/cad/allocatePath`, {
      documentUuid,
      format,
    });
  },
};
